/**
 * gameManager
 * @Author Ace.c
 * @Create 2016-11-29 11:52
 */
class gameManager extends NotifyEvent {

    private static _i: gameManager;

    static get i(): gameManager {
        return this._i || (this._i = new gameManager());
    }

    static timerManager: TimerManager;
    static layerManager: LayerManager;

    static socketManager: SocketManager;
    static msgManager: MsgManager;

    static effectManager: EffectManager;
    static viewManager: ViewManager;

    static dataManager: DataManager;

    //公告
    static notice: NoticeView;

    //公告计时
    static notice_time: number = 0;
    //公告索引
    static notice_index: number = 0;
    //聊天计时
    static chatTime: number = 0;
    //禁用按钮计时
    static disableBtnTime: number = 0;

    constructor() {
        super();
    }

    static init() {
        gameManager.timerManager = TimerManager.i;
        gameManager.layerManager = LayerManager.i;

        gameManager.socketManager = SocketManager.i;
        gameManager.msgManager = new MsgManager();

        gameManager.effectManager = new EffectManager();
        gameManager.viewManager = new ViewManager();

        gameManager.dataManager = new DataManager();

        this.openSceneLayer();

        this.notice = new NoticeView();
        gameManager.notice_time = GameConfig.notice_time;
        gameManager.timerManager.addEventListener(TimerManager.TIMER, this.timerHandler, this);

        acelab.stage.addEventListener(egret.TouchEvent.TOUCH_TAP, this.stageClickHandler, this);
        acelab.stage.addEventListener(egret.Event.RESIZE, this.resizeHandler, this);
    }

    private static stageClickHandler(e:egret.TouchEvent) {
        //点击舞台,播放背景音乐(ios不能自动加载音乐)
        GameMusic.play("music_scene");
    }

    private static resizeHandler() {
        PopUpUtils.redrawMasker();
    }

    private static timerHandler() {
        if (GameConfig.notice_list.length && ++this.notice_time >= GameConfig.notice_time) {
            this.notice_time = 0;
            this.notice_index = this.notice_index >= GameConfig.notice_list.length ? 0 : this.notice_index;

            let data = GameConfig.notice_list[this.notice_index++];
            if (typeof data == 'string') {
                gameManager.notice.play(data);
            }
            else {
                if (data.hasOwnProperty('color')) {
                    gameManager.notice.play(data.desc, data.color);
                }
                else {
                    gameManager.notice.play(data.desc);
                }
            }
        }

        if (gameManager.chatTime > 0) gameManager.chatTime--;

        if (game.isBtnDisabledByTime) {
            if (gameManager.disableBtnTime < GameConst.Time_DisableBtn) {
                gameManager.disableBtnTime++;
            }
            else {
                game.isBtnDisabledByTime = false;
                gameManager.disableBtnTime = 0;
            }
        }
    }

    static find(name: string): any {
        return gameManager.viewManager.find(name);
    }

    static open(name: string, otherClose: boolean = true, eff: number = 1, dark: boolean = true): any {
        return gameManager.viewManager.open(name, otherClose, eff, dark);
    }

    static close(name: string) {
        gameManager.viewManager.close(name);
    }

    //打开麻将界面层级，将自动关闭主界面层级
    static openMainLayer(): void {
        this.layerManager.sceneLayer.visible = false;
        this.layerManager.mainLayer.visible = true;

        GSController.i.jiesuanData = null;
    }

    //打开主界面层级，将自动关闭麻将层级
    static openSceneLayer(): void {
        this.layerManager.sceneLayer.visible = true;
        this.layerManager.mainLayer.visible = false;

        Weixin.shareGame();
        Weixin.shareGameToTimeline();
    }
}